<?php
function genotype_details_display($id)
{

	$tbl_header = array();

	$query = db_select('genotypes', 'g');
	$query->join('users', 'u', 'u.uid = g.us_id');
	$query->join('gene_pool', 'p', 'p.id = g.pool_id');
	$genotypes = $query
		->fields('g', array('id', 'date', 'name', 'count_p', 'count_j', 'count_n', 'count_c', 'shortinfo', 'originstory', 'notes', 'geno_html', 'geno_F0', 'reviewed_by'))
		->fields('u', array('name'))
		->fields('p', array('name'))
		->condition('g.id', $id)
		->execute();
	$genotype = $genotypes->fetchAssoc();

	if (empty($genotype))
	{
		drupal_set_message(t('No such genotype'));
		return "No such genotype";
	}
	
	$genoF0 = "<!--FRED_GEN" . $genotype['geno_f0'] . "-->";

	//Name
	  $rows[] = array(
	    array('data' => t('Name'), 'header' => true),
	    array('data' => $genotype['name'] ));
	
	//From gene pool
	//Odkomentowac jak pojawi sie cos takiego w bazie
	  $rows[] = array(
	    array('data' => t('Gene pool'), 'header' => true),
	    array('data' => $genotype['p_name'] ));

	//Author
	  $rows[] = array(
	    array('data' => t('Author'), 'header' => true),
	    array('data' => $genotype['u_name'] ));
	
	//Date submitted
	//Odkomentowac jak sie pojawi cos takiego w bazie
	  $rows[] = array(
	    array('data' => t('Date submitted'), 'header' => true),
	    array('data' => date('d F Y h:i:s A', $genotype['date'])));

	//Size
	  $rows[] = array(
	    array('data' => t('Size'), 'header' => true),
	    array('data' => $genotype['count_p'] . t(' parts') . '<br>' .
	                        $genotype['count_j'] . t(' joints') . '<br>' .
	                        $genotype['count_n'] . t(' neurons') . '<br>' .
	                        $genotype['count_c'] . t(' neural connections')));
	
	$applet = "<applet archive=\"Favi.jar\" code=\"favi/Favi.class\" codebase=\"favi\" height=\"20\" width=\"60\" align=\"middle\">" .
				"<param name=\"button\" value=\"view\">" .
				"<param name=\"genotype\" value=\"" . $genotype['geno_f0'] . "\">" .
				"</applet>";
//				"<param name=\"DB_URL\" value=\"?q=genotype_details/" . $genotype['id'] . "\"></applet>";

	//Actions
	  $rows[] = array(
	    array('data' => t('Actions'), 'header' => true),
	    array('data' => l('download', "genotype_download/". $genotype['id']). "<br/>" . $applet )
	);
	
	//Description
	  $rows[] = array(
	    array('data' => t('Description'), 'header' => true),
	    array('data' => $genotype['shortinfo'] ));
	
	//How created
	  $rows[] = array(
	    array('data' => t('How created'), 'header' => true),
	    array('data' => $genotype['originstory'] ));
	
	//Performance notes
	  $rows[] = array(
	    array('data' => t('Performance notes'), 'header' => true),
	    array('data' => $genotype['notes'] ));
	
	
	//Fill genotype
	  $rows[] = array(
	    array('data' => t('Full genotype'), 'header' => true),
	    array('data' => $genotype['geno_html'] ));

	if (empty($genotype['reviewed_by']))
	{
		if (user_access('review genotype'))
		{
			$rows[] = array(
			    array('data' => t('Reveiw genotype'), 'header' => true),
			    array('data' => l("Aprove", 'genotype_review/'.$genotype['id']) . "<br/>" )
			);
		}
	}
	else
	{
		$query = db_select('users', 'u')
			-> fields('u', array('name'))
			-> condition('uid', $genotype['reviewed_by']);
		$user = $query->execute();

		$user = $user->fetchAssoc();
		if (!empty($user))
		{
			$rows[] = array(
			    array('data' => t('Reveiwed by'), 'header' => true),
			    array('data' => $user['name'] )
			);
		}
	}

	$variables = array(
	  "header" => $tbl_header,
	  "rows" => $rows,
	  "attributes" => array(),
	  "caption" => "",
	  "colgroups" => array(),
	  "sticky" => "",
	  "empty" => "");
	
	$html = theme_table($variables);
	return $html.$genoF0;
}

function genotype_view_display($id)
{
	$query = db_select('genotypes', 'g');
	$genotypes = $query
		->fields('g', array('geno_F0'))
		->condition('g.id', $id)
		->execute();
	$genotype = $genotypes->fetchAssoc();

	if (empty($genotype))
	{
		drupal_set_message(t('No such genotype'));
		return "No such genotype";
	}

	echo $genotype['geno_f0'];
	die();
}

function genotype_review_submit($id)
{
	global $user;
	if (user_access('review genotype'))
	{
		db_update('genotypes')
			->condition('id', $id)
			->fields(array('reviewed_by' => $user->uid))
			->execute();
		drupal_set_message("Genotype approved correctly");
		return "OK";
	}
	else
	{
		return "No permissions to review genotypes";
	}
}

?>

